package com.asset.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.asset.common.ErrorCode;
import com.asset.constant.UserConstant;
import com.asset.exception.ThrowUtils;
import com.asset.mapper.AssetMapper;
import com.asset.model.dto.asset.AssetQueryRequest;
import com.asset.model.vo.AssetDetailsVO;
import com.asset.model.vo.AssetVO;
import com.asset.model.vo.LoginUserVO;
import com.asset.service.AssetService;
import com.asset.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class AssetServiceImpl implements AssetService {

    @Resource
    private AssetMapper assetMapper;
    
    @Resource
    private UserService userService;

    @Override
    public PageInfo<AssetVO> getAssetListVOPage(AssetQueryRequest assetQueryRequest) {
        // 1. 设置分页参数（当前页、每页条数）
        PageHelper.startPage(assetQueryRequest.getCurrent(), assetQueryRequest.getPageSize());
        // 获取登录者公司id
        LoginUserVO loginUser = (LoginUserVO)StpUtil.getSession().get(UserConstant.USER_LOGIN_STATE);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.PARAMS_ERROR);
        // 获取登录者选择的 公司编码
        String companyCode = assetQueryRequest.getCompanyCode();
        // 获取登录人所在的公司编码
        String loginUserCompanyCode = loginUser.getCompanyCode();
        ThrowUtils.throwIf(!companyCode.contains(loginUserCompanyCode),ErrorCode.NO_AUTH_ERROR);
        
//        int loginUserCompanyId = loginUser.getCompanyId();
//        assetQueryRequest.setCompanyId(userService.getLoginUserVOByTK().getCompanyId());
        // 根据传入的三权筛选条件进行查询
        Integer companyId = assetQueryRequest.getCompanyId();// 登录者选择的公司ID
        Integer ThreeId = assetQueryRequest.getThreeCompanyId();
        Integer ownershipCompanyId = null;  // 强制初始化
        Integer managementCompanyId = null;  // 强制初始化
        Integer usageCompanyId = null;  // 强制初始化

        switch (ThreeId) {
            case 1:
                ownershipCompanyId = companyId;
                break;
            case 2:
                managementCompanyId = companyId;
                break;
            case 3:
                usageCompanyId = companyId;
                break;
            default:
                ownershipCompanyId = companyId;
                break;
        }
        System.out.println("产权: " + ownershipCompanyId );
        System.out.println("管理权: " + managementCompanyId );
        System.out.println("使用权: " + usageCompanyId );



        System.out.println("ownershipCompanyId: " + ownershipCompanyId);
        System.out.println("managementCompanyId: " + managementCompanyId);
        System.out.println("usageCompanyId: " + usageCompanyId );
        // 2. 调用 Mapper 查询（自动分页）
        List<AssetVO> assets = assetMapper.selectAssetListByConditionPage(
                assetQueryRequest.getAssetCode(),   // 资产编号
                assetQueryRequest.getName(),  // 资产名称
                assetQueryRequest.getAssetType(),   // 资产类型
                assetQueryRequest.getLocation(),    // 位置
                ownershipCompanyId,  // 所有者公司ID
                managementCompanyId,  // 管理公司ID
                usageCompanyId,  // 使用公司ID
                assetQueryRequest.getStatus(),  // 资产状态
                assetQueryRequest.getStartCreateTime(),  // 创建时间开始
                assetQueryRequest.getEndCreateTime()  // 创建时间结束
        );

        // 3. 转换为 PageInfo 获取分页信息
        //PageInfo<AssetVO> pageInfo = new PageInfo<>(assets);

        // 4. 将 Asset 转换为 AssetVO
        List<AssetVO> collect = assets.stream()
                .map(asset -> {
                    AssetVO vo = new AssetVO();
                    vo.setId(asset.getId());   //  ID
                    vo.setAssetCode(asset.getAssetCode());  // 资产编号
                    vo.setName(asset.getName());   // 资产名称
                    vo.setCategoryType(asset.getCategoryType());  // 资产类型
                    vo.setLocation(asset.getLocation());   // 位置
                    vo.setSpecification(asset.getSpecification());  //  规格
                    vo.setOwnershipCompany(asset.getOwnershipCompany());  //  产权公司
                    vo.setManagementCompany(asset.getManagementCompany());  //  管理公司
                    vo.setUsageCompany(asset.getUsageCompany());  //  使用公司
                    vo.setStatus(asset.getStatus());  //  状态
                    vo.setCreatedAt(asset.getCreatedAt());  //  创建时间
                    return vo;
                })
                .collect(Collectors.toList());// 转换为列表
        return new PageInfo<AssetVO>(collect);
    }

    @Override
    public AssetDetailsVO getAssetById(Integer id) {
        AssetDetailsVO assetDetailsVO =  assetMapper.selectAssetById(id);
        return assetDetailsVO;
    }
}
